const db = require('./db-util')

let sql_select="SELECT * FROM product";
let sql_selectByName = "SELECT * FROM demo.product\n" +
    "where product_name =?";
let sql_insert = "insert into(product_name,desc) value(?,?)";
let sql_update ="update product set product_name=?,`desc`=? where product_id=?"
let sql_delete = "delete from product where product_id=?";


function selectByName(productName,callback) {

    db.query(sql_selectByName,[productName],(err,rows)=>{
        callback(rows);
    })
}

function select(callback) {

    db.query(sql_select,null,(err,rows)=>{
        console.log(rows);
        callback(rows)
    })
}

function insert(product,callback) {

     const params =[];
     params.push(product.productName);
     params.push(product.desc);

    db.query(sql_insert,params,(rows)=>{
        callback(rows.affectedRows)
    })
}


function update(product,callback) {
    const params = [];
    if(product.productName){
        params.push(product.productName);
    }
    if(product.desc){
        params.push(product.desc);
    }
    db.query(sql_update,params,(rows)=>{
        callback(rows.affectedRows);
    })
}

function remove(productId,callback) {

    db.query(sql_delete,productId,(rows)=>{
        callback(rows.affectedRows);
    })
}

module.exports = {
    selectByName,
    select,
    insert,
    update,
    remove
}

